{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.7 偏导数\n",
    "多元函数偏导与全微分(第65页)\n",
    "\n",
    "例1：设函数z=(x,y)由方程z=e*(2x-3y)+2y确定求3(∂z∂x+∂z∂y)|x=3,y=2的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, exp, diff, lambdify\n",
    "\n",
    "# 定义变量\n",
    "x, y = symbols('x y')\n",
    "\n",
    "# 定义函数\n",
    "f = exp(2*x - 3*y) + 2*y\n",
    "\n",
    "# 计算偏导数\n",
    "df_dx = f.diff(x)\n",
    "df_dy = f.diff(y)\n",
    "\n",
    "# 计算指定点的偏导数值并求和\n",
    "value = 3 * (df_dx.subs({x: 3, y: 2}) + df_dy.subs({x: 3, y: 2}))\n",
    "print(value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求函数\n",
    "z=cos√(x2+y2)的偏导数∂z∂x,∂z∂y,∂2z∂x∂y,全微分dz以及dz|x=1,y=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "&part;z/&part;x: -x*sin(sqrt(x**2 + y**2))/sqrt(x**2 + y**2)\n",
      "&part;z/&part;y: -y*sin(sqrt(x**2 + y**2))/sqrt(x**2 + y**2)\n",
      "&part;z/&part;x at (1, 2): -sqrt(5)*sin(sqrt(5))/5\n",
      "&part;z/&part;y at (1, 2): -2*sqrt(5)*sin(sqrt(5))/5\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, cos, sqrt, diff\n",
    "\n",
    "# 定义变量\n",
    "x, y = symbols('x y')\n",
    "\n",
    "# 定义函数\n",
    "f = cos(sqrt(x**2 + y**2))\n",
    "\n",
    "# 计算偏导数\n",
    "df_dx = f.diff(x)\n",
    "df_dy = f.diff(y)\n",
    "\n",
    "# 输出偏导数\n",
    "print(\"&part;z/&part;x:\", df_dx)\n",
    "print(\"&part;z/&part;y:\", df_dy)\n",
    "\n",
    "# 计算指定点的偏导数值\n",
    "df_dx_value = df_dx.subs({x: 1, y: 2})\n",
    "df_dy_value = df_dy.subs({x: 1, y: 2})\n",
    "\n",
    "# 输出指定点的偏导数值\n",
    "print(\"&part;z/&part;x at (1, 2):\", df_dx_value)\n",
    "print(\"&part;z/&part;y at (1, 2):\", df_dy_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多元函数极值与最值（第111页）\n",
    "\n",
    "例：求函数\n",
    "f(x,y)=x2+y2\n",
    "的极值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{x: 0, y: 0}\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, diff, solve\n",
    "\n",
    "# 定义变量\n",
    "x, y = symbols('x y')\n",
    "\n",
    "# 定义函数\n",
    "f = x**2 + y**2\n",
    "\n",
    "# 求偏导数并解方程组\n",
    "critical_points = solve([f.diff(x), f.diff(y)], (x, y))\n",
    "print(critical_points)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.8 重积分\n",
    "\n",
    "4.8.1 重积分的计算（第135页）\n",
    "\n",
    "例1：计算积分∫20dx∫2xe−y2dy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-(1 - exp(4))*exp(-4)/2\n"
     ]
    }
   ],
   "source": [
    "from sympy import integrate, exp\n",
    "\n",
    "# 计算重积分\n",
    "result = integrate(integrate(exp(-y**2), (y, x, 2)), (x, 0, 2)).simplify()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例：计算三重积分\n",
    "∭Ωzdxdydz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/24\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, integrate\n",
    "\n",
    "x, y, z = symbols('x y z')\n",
    "\n",
    "# 定义积分\n",
    "inner_integral = integrate(z, (z, 0, 1-x-y))\n",
    "middle_integral = integrate(inner_integral, (y, 0, 1-x))\n",
    "outer_integral = integrate(middle_integral, (x, 0, 1))\n",
    "\n",
    "print(outer_integral)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.8.2 重积分的应用\n",
    "示例 1: 求曲面 z=x²+y² 和 z=2−x²−y² 围成的体积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6*pi\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, integrate, pi, sqrt\n",
    "\n",
    "x, y, r, theta = symbols('x y r theta')\n",
    "\n",
    "# 定义积分\n",
    "inner_integral = integrate(6*r - 3*r**3, (r, 0, sqrt(2)))\n",
    "volume_integral = integrate(inner_integral, (theta, 0, 2*pi))\n",
    "\n",
    "print(volume_integral)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "示例 2: 求 ∬D (x² + y) dxdy，其中 D 是由抛物线 y=x² 和 x=y² 所围平面闭区域"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "33/140\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, integrate\n",
    "\n",
    "x, y = symbols('x y')\n",
    "\n",
    "# 定义积分\n",
    "inner_integral = integrate(x**2 + y, (y, x**2, sqrt(x)))\n",
    "outer_integral = integrate(inner_integral, (x, 0, 1))\n",
    "\n",
    "print(outer_integral)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "曲线积分\n",
    "示例 1: 设 L 为 {x = e^t + 1, y = e^t - 1} 从 t=0 到 log(2) 的一段弧，求曲线积分 ∫_L xdx + ydy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, integrate, exp, ln\n",
    "\n",
    "t = symbols('t')\n",
    "x = exp(t) + 1\n",
    "y = exp(t) - 1\n",
    "\n",
    "dx = x.diff(t)\n",
    "dy = y.diff(t)\n",
    "\n",
    "integral = integrate(x * dx + y * dy, (t, 0, ln(2)))\n",
    "\n",
    "print(integral)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "曲面积分\n",
    "示例 1: 计算 ∬_Σ x²dydz + y²dzdx + zdxdy，其中 Σ 是旋转抛物面 z=1−x²−y²(z≥0) 的上侧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pi/3\n"
     ]
    }
   ],
   "source": [
    "from sympy import symbols, integrate, cos, sin, pi\n",
    "\n",
    "r, theta = symbols('r theta')\n",
    "\n",
    "# 内层积分\n",
    "inner_integral = integrate((2*r*cos(theta) + 2*r*sin(theta) + 1)*(1 - r**2)*r, (z, 0, 1-r**2))\n",
    "\n",
    "# 中间层积分\n",
    "middle_integral = integrate(inner_integral, (r, 0, 1))\n",
    "\n",
    "# 外层积分\n",
    "outer_integral = integrate(middle_integral, (theta, 0, 2*pi))\n",
    "\n",
    "print(outer_integral)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
